package com.dc.trans.steps.checkinforepeat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.Counter;
import org.pentaho.di.core.RowMetaAndData;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.shared.SharedObjectInterface;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.w3c.dom.Node;


public class CheckInfoRepeatMeta extends BaseStepMeta implements StepMetaInterface {
	
	private static Class<?> PKG = CheckInfoRepeatMeta.class;
	private String stepName;
	private String checkField;
	
	@Override
	public void readRep(Repository rep, ObjectId id_step, List<DatabaseMeta> databases, Map<String, Counter> counters)
			throws KettleException {
		try {
			checkField= rep.getStepAttributeString(id_step, "checkField");
		} catch (Exception e) {
			throw new KettleException("Unexpected error reading step information from the repository", e);
		}
		
	}
	
	@Override
	public void saveRep(Repository rep, ObjectId id_transformation,
			ObjectId id_step) throws KettleException {
		try {
			rep.saveStepAttribute(id_transformation, id_step, "checkField", checkField);
		} catch (Exception e) {
			throw new KettleException("Unable to save step information to the repository for id_step=" + id_step, e);
		}
		
	}
	
	public void loadXML(Node stepnode, List<DatabaseMeta> databases, Map<String, Counter> counters)
			throws KettleXMLException {
		readData(stepnode, databases);
	}

	public Object clone() {
		CheckInfoRepeatMeta retval = (CheckInfoRepeatMeta) super.clone();
		return retval;
	}
	/**
	 * @param useBatchUpdate
	 *            The useBatchUpdate flag to set.
	 */
	private void readData(Node stepnode, List<? extends SharedObjectInterface> databases) throws KettleXMLException {
		try {
			checkField = XMLHandler.getTagValue(stepnode, "checkField");
		} catch (Exception e) {
			throw new KettleXMLException("Unable to load step info from XML", e);
		}
	}

	public void setDefault() {
	}

	public String getXML() {
		StringBuffer retval = new StringBuffer();
		retval.append("    " ).append( XMLHandler.addTagValue("checkField", checkField));
		return retval.toString();
	}
	

	public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep,
			VariableSpace space) throws KettleStepException {
		List<CheckResultInterface> remarks = new ArrayList<CheckResultInterface>();
		RowMetaAndData rowMetaAndData = CheckInfoRepeat.buildRow(this, remarks, origin);
		if (!remarks.isEmpty()) {
			StringBuffer stringRemarks = new StringBuffer();
			for (CheckResultInterface remark : remarks) {
				stringRemarks.append(remark.toString()).append(Const.CR);
			}
			throw new KettleStepException(stringRemarks.toString());
		}
		
		for (ValueMetaInterface valueMeta : rowMetaAndData.getRowMeta().getValueMetaList()) {
			valueMeta.setOrigin(origin);
		}
		
		row.mergeRowMeta(rowMetaAndData.getRowMeta());
	}

	public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta,
			RowMetaInterface prev, String input[], String output[], RowMetaInterface info) {

		// See if we have input streams leading to this step!
		if (input.length > 0) {
			CheckResult cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG,"CompressMeta.CheckResult.ExpectedInputOk"), stepMeta);
			remarks.add(cr);
		} else {
			CheckResult cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG,"CompressMeta.CheckResult.ExpectedInputError"), stepMeta);
			remarks.add(cr);
		}
	}

	
	public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta transMeta,
			Trans trans) {
		return new CheckInfoRepeat(stepMeta, stepDataInterface, cnr, transMeta, trans);
	}

	public StepDataInterface getStepData() {
		return  new CheckInfoRepeatData();
	}

	public boolean supportsErrorHandling() {
		return true;
	}

	public String getStepName() {
		return stepName;
	}

	public void setStepName(String stepName) {
		this.stepName = stepName;
	}

	public static Class<?> getPKG() {
		return PKG;
	}

	public static void setPKG(Class<?> pkg) {
		PKG = pkg;
	}

	public String getCheckField() {
		return checkField;
	}

	public void setCheckField(String checkField) {
		this.checkField = checkField;
	}


}
